<?php
namespace app\index\controller;

use think\Request;
use app\index\model\VisitorModel;
use app\index\model\NavModel;

class Visitor extends Base {
	public function _initialize() {
		parent::_initialize();
		if(session('visitor_id') == ""){
			$this->error("请先登录",url('index/index/index'));
		}
	}

	public function index() {
		$navModel = new NavModel();
		$sideBar = $navModel::where('pid',22)->order(['sort asc','id asc'])->select()->toArray();
		foreach($sideBar as &$v){
			$v['children'] = $navModel::where('pid',$v['id'])->order(['sort asc','id asc'])->select()->toArray();
		}
		$this->assign([
			'SideBar' => $sideBar
		]);
		return $this->fetch();
	}

    public function index_page(){
		$visitorModel = new VisitorModel();
		$visitor = $visitorModel::where('id',session('visitor_id'))->find();
		if(empty($visitor)){
			$this->error("参数错误",'index/index');
		}else{
			$visitor = $visitor->toArray();
		}
		$this->assign([
			'Visitor' => $visitor,
		]);
		return $this->fetch();
    }

	/**
	 * [游客头像上传]
	 */
	public function uploadHead() {
		$visitorModel = new VisitorModel();
		$old_file = $visitorModel->where('id',session('visitor_id'))->value('head');
		$old_file = APP_PATH."../public".$old_file;
		$old_file = iconv("UTF-8", "GBK", $old_file);
		if(file_exists($old_file)){
			@unlink($old_file);
		}
		if(!isset($_FILES["Filedata"])||empty($_FILES["Filedata"])){
            return ['code'=>0,'msg'=>'没有上传文件'];
        }else{
			$ext = explode(".",$_FILES['Filedata']['name'])[1];
			$tmp = $_FILES['Filedata']['tmp_name'];
			$name = session("visitor_id")."_".date("YmdHis");
			$path = "upload/visitor_head";
			$save = $path."/".$name.".".$ext;
			try {
				if (!is_dir($path)) {
					mkdir(iconv("UTF-8", "GBK", $path), 0777, true);
				}
				if (move_uploaded_file($tmp, iconv("UTF-8", "GBK", $save))) {
					$res = $visitorModel->where('id',session('visitor_id'))->update(['head'=>"/".$save]);
					if($res !== false){
						session("visitor_head","/".$save);
						return json(['code'=>1,'msg'=>'头像修改成功']);
					}else{
						$default_save = "/static/admin/images/user.png";
						$visitorModel->where('id',session('visitor_id'))->update(['head'=>$default_save]);
						session("visitor_head",$default_save);
						return json(['code'=>0,'msg'=>'头像修改失败：无法写入数据库']);
					}
				}
			} catch (\Exception $e) {
				return json(['code'=>0,'msg'=>'头像修改失败:'.$e->getMessage()]);
			}
		}
	}

	public function editInfo() {
		$visitorModel = new VisitorModel();
		if($this->request->isPost()){
			$param = input("post.");
			$res = $visitorModel::where('id',session("visitor_id"))->update($param);
			if($res !== false){
				return ['code'=>1,'msg'=>'保存成功'];
			}
			return ['code'=>0,'msg'=>'保存失败'];
		}
		$visitor = $visitorModel::where('id',session("visitor_id"))->find();
		$this->assign([
			'Visitor' => $visitor,
		]);
		return $this->fetch();
	}

	/**
	 * [安全问题]
	 */
	public function safety_question() {

	}

	/**
	 * [真实信息]
	 */
	public function real_info() {

	}

	/**
	 * [账号找回]
	 */
	public function lost_and_found() {

	}

	/**
	 * [账号申诉]
	 */
	public function appeal() {

	}
}
